<template>
    <div class="MemberRegistration">
        <Addtitle>房屋成员登记</Addtitle>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <div class="basicinfomation">
                <p class="title">基础信息</p>
                <div class="infomation">
                    <div class="inputbox">
                        <el-form-item label="关联房屋：" prop="housename" class="input">
                            <el-input disabled v-model="ruleForm.housename" placeholder="万达文旅城/1号楼/1单元/2605"></el-input>
                        </el-form-item>
                        <el-form-item label="业主姓名：" prop="name" class="input">
                            <el-input disabled v-model="ruleForm.name" placeholder="请输入业主姓名"></el-input>
                        </el-form-item>
                        <el-form-item label="业主身份证号：" prop="IDcard" class="input" label-width="120px" >
                            <el-input disabled v-model="ruleForm.IDcard" placeholder="请输入身份证号"></el-input>
                        </el-form-item>
                    </div>
                    <div class="inputbox">
                        <el-form-item  label="业主类型：" prop="Residentialtype">
                            <el-select disabled v-model="ruleForm.Residentialtype" placeholder="请选择业主类型">
                            <el-option label="长局" value="shanghai"></el-option>
                            <el-option label="出租" value="beijing"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="业主手机号：" prop="phonenumber" class="input">
                            <el-input disabled v-model="ruleForm.phonenumber" placeholder="请输入业主手机号"></el-input>
                        </el-form-item>
                        <el-form-item label="户籍所在地：" prop="Registeredresidence" class="input">
                            <el-input disabled v-model="ruleForm.Registeredresidence" placeholder="请输入户籍所在地"></el-input>
                        </el-form-item>
                    </div>
                </div>
            </div>
            <div class="memberfomation">
                <div class="header">
                    <p class="title">成员信息</p>
                    <p class="addmenmber" @click="addDomain()">+添加新成员</p>
                </div>
                <!-- <div class="addbox">
                    <el-form-item label="联系人姓名：" prop="name" class="add">
                        <el-input v-model="ruleForm.name"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号码：" prop="name" class="add">
                        <el-input v-model="ruleForm.name"></el-input>
                    </el-form-item>
                    <el-form-item label="业主身份证号：" prop="name" class="add">
                        <el-input v-model="ruleForm.name"></el-input>
                    </el-form-item>
                    <el-form-item label="关系：" prop="region">
                        <el-select v-model="ruleForm.region" placeholder="请选择活动区域">
                        <el-option label="区域一" value="shanghai"></el-option>
                        <el-option label="区域二" value="beijing"></el-option>
                        </el-select>
                    </el-form-item>
                </div> -->
                <!-- <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="120px" class="demo-dynamic"> -->
                <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="120px" class="demo-dynamic">
                <div class="addbox" v-for="(domain, index) in dynamicValidateForm.domains" :key="index">
                    <el-form-item
                    label-width="120px"
                    label="联系人姓名："
                    
                    :prop="'domains.' + index + '.householdName'"
                    :rules="{
                        required: true, message: '姓名不能为空', trigger: 'blur'
                    }"
                >
                    <el-input v-model="domain.householdName"></el-input>
                </el-form-item>
                <el-form-item
                    label="手机号码："
                    label-width="100px"
                    :prop="'domains.' + index + '.householdPhone'"
                    :rules="[
                        {
                            required: true, message: '手机号不能为空', trigger: 'blur'
                        },
                        {
                            min:9,max:11, message: '请输入9-11位有效手机号', trigger: 'blur'
                        }
                    ]"
                >
                    <el-input v-model="domain.householdPhone"></el-input>
                </el-form-item>
                <el-form-item
                    label="业主身份证号："
                    :prop="'domains.' + index + '.householeIDcard'"
                    :rules="[
                        {
                            required: true, message: '身份证号不能为空', trigger: 'blur'
                        },
                        {
                            min:18,max:18, message: '请输入18位有效身份证号', trigger: 'blur'
                        }
                    ]"
                >
                    <el-input v-model="domain.householeIDcard"></el-input>
                </el-form-item>
                <el-form-item
                    label="关系："
                    label-width="80px"
                    :prop="'domains.' + index + '.householdType'"
                    :rules="{
                    required: true, message: '关系不能为空', trigger: 'blur'
                    }"
                >
                    <el-select v-model="domain.householdType" placeholder="与户主关系">
                        <el-option label="家人" value="家人"></el-option>
                        <el-option label="朋友" value="朋友"></el-option>
                        <el-option label="租户" value="租户"></el-option>
                    </el-select>
                </el-form-item>
                <el-button @click.prevent="removeDomain(domain)" class="deabutton">删除</el-button>
                </div>
                </el-form>
            </div>
            <div class="buttonbox">
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm','dynamicValidateForm')" class="savebutton">立即创建</el-button>
                    <el-button @click="resetForm('ruleForm','dynamicValidateForm')" class="resetbutton">重置</el-button>
                </el-form-item>
            </div>
        </el-form>
    </div>
</template>

<script>
import Addtitle from '../components/Addtitle.vue'
export default {
    components:{
        Addtitle
    },
    data() {
      return {
        dynamicValidateForm: {
          domains: [{
            householdName: '',  //住户姓名
            householdPhone:'',  //住户手机号
            householeIDcard:'', //住户身份证
            householdType:'',   //与业主关系
          }],
        },
        ruleForm: {
          housename:"", //关联房屋
          name:'', //业主姓名
          IDcard:"",   //业主身份证号
          Residentialtype:'',//业主类型
          phonenumber:'',       //业主手机号
          Registeredresidence:'',   //户籍所在地
        },
        rules: {
            housename:[
                { required: true, message: '请输入关联房屋', trigger: 'change' }
            ],
            name: [
                { required: true, message: '请输入业主姓名', trigger: 'blur' },
                { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
            ],
            IDcard:[
                { required: true, message: '请输入身份证号', trigger: 'change' },
                { min: 18, max: 18, message: '请输入18位有效身份证号', trigger: 'blur' }
            ],
            Residentialtype:[
                { required: true, message: '请输入业主类型', trigger: 'change' }
            ],
            phonenumber:[
                { required: true, message: '请输入手机号', trigger: 'change' },
                { min: 9, max: 11, message: '请输入9-11位有效手机号', trigger: 'blur' }
            ],
            Registeredresidence:[
                { required: true, message: '请输入户籍所在地', trigger: 'change' }
            ],
        }
      };
    },
    mounted(){
        console.log(this.$route.query.data.date)
        this.$axios.post('http://127.0.0.1/householdselect',{
            date:this.$route.query.data.date
        }).then(res=>{
            console.log(res.data.data,33333333333)
            this.dynamicValidateForm.domains=res.data.data
        }).catch(err=>{
            console.log(err)
        })
        // this.dynamicValidateForm.domains=this.$route.query.a.domains
        this.ruleForm=this.$route.query.data
        this.ruleForm.housename=this.$route.query.data.Belongingcommunity+'/'+this.$route.query.data.Buildingname+'/'+this.$route.query.data.Numberofunits+'单元/'+this.$route.query.data.RoomNo
    },
    methods: {
        // 删除
        removeDomain(item) {
            console.log(item)
            var index = this.dynamicValidateForm.domains.indexOf(item)
            if(item.household_id){
                console.log('删除数据库',item.household_id)
                this.$axios.post('http://127.0.0.1/householddelete',{
                    id:item.household_id
                }).then(res=>{
                    console.log(res)
                    this.dynamicValidateForm.domains.splice(index, 1)
                }).catch(err=>{
                    console.log(err)
                })
            }else{
                console.log('删除数组')
                this.dynamicValidateForm.domains.splice(index, 1)
            }
            // var index = this.dynamicValidateForm.domains.indexOf(item)
            // if (index !== -1) {
            //     this.dynamicValidateForm.domains.splice(index, 1)
            // }
        },
        // 添加住户列表
        addDomain() {
            this.dynamicValidateForm.domains.push({
                householdName: '',  //住户姓名
                householdPhone:'',  //住户手机号
                householeIDcard:'', //住户身份证
                householdType:'',   //与业主关系
                key: Date.now()
            });
        },
        // 保存
        submitForm(formName,dynamicValidateForm) {
            // console.log(this.dynamicValidateForm.domains)
            
            // this.$refs[formName].validate((valid) => {
            //     if (valid) {
            //         alert('submit!','formName');
            //     } else {
            //         console.log('error submit!!');
            //         return false;
            //     }
            // });
            // // 住户
            this.$refs[dynamicValidateForm].validate((valid) => {
                if (valid) {
                    this.$axios.post('http://127.0.0.1/householdinsert',{
                        ...this.dynamicValidateForm,
                        date:this.$route.query.data.date
                    }).then(res=>{
                        console.log(res,'返回上一级')
                        this.$router.go(-1)
                        this.$message({
                            type: 'success',
                            message: '数据修改成功!'
                        });
                    }).catch(err=>{
                        console.log(err)
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        // 重置
        resetForm(formName,dynamicValidateForm) {
            this.$refs[formName].resetFields();
            this.$refs[dynamicValidateForm].resetFields();
        }
    }
}
</script>

<style lang="scss" scoped>
    .MemberRegistration{
        width: 1260px;  /* 最大1265px */ 
        margin: auto;
        height: 80vh;
        overflow: scroll;
        scrollbar-width: none;
        background-color: #fff;
        border: 1px solid rgba(233, 233, 233, 1);
        .demo-ruleForm{
            padding: 20px 60px 20px 40px;
            box-sizing: border-box;
            .basicinfomation{
                .title{
                    width: 550px;
                    height: 20px;
                    line-height: 20px;
                    font-size: 14px;
                    color: #666666;
                    padding-left: 5px;
                    font-family: '微软雅黑 Bold', '微软雅黑 Regular', '微软雅黑';
                    font-weight: 700;
                    border-left: 4px solid rgba(0, 121, 254, 1);
                }
                .infomation{
                    width: 100%;
                    margin-top: 15px;
                    display: flex;
                    justify-content: space-between;
                    .input{
                        width: 520px;
                    }
                }
            }
            .memberfomation{
                margin-top: 25px;
                .header{
                    width: 100%;
                    margin-bottom: 20px;
                    display: flex;
                    justify-content: space-between;
                    .title{
                        width: 550px;
                        height: 20px;
                        line-height: 20px;
                        font-size: 14px;
                        color: #666666;
                        padding-left: 5px;
                        font-family: '微软雅黑 Bold', '微软雅黑 Regular', '微软雅黑';
                        font-weight: 700;
                        border-left: 4px solid rgba(0, 121, 254, 1);
                    }
                    .addmenmber{
                        color: #3399FF;
                        font-size: 14px;
                        cursor: pointer;
                    }
                }
                .addbox{
                    width: 100%;
                    display: flex;
                    justify-content: space-between;
                    .deabutton{
                        width: 50px;
                        height: 40px;
                        padding: 0 !important;
                        margin-left: 15px;
                    }
                    .deabutton:hover{
                        background-color: #ecf5ff;
                        color: #409eff;
                    }
                }
            }
            .buttonbox{
                width: 100%;
                height: 100px;
                display: flex;
                justify-content: center;
                align-items: center;
                border-top: 1px solid rgb(242,242,242);
                ::v-deep .el-form-item__content{
                    margin-left: 0 !important;
                }
                .savebutton{
                    width: 140px;
                    height: 40px;
                    line-height: 40px;
                    padding: 0 !important;
                    background-color: #0079fe;
                    cursor: pointer;
                }
                .resetbutton{
                    width: 140px;
                    height: 40px;
                    line-height: 40px;
                    border-color: #0079fe;
                    color: #0079fe;
                    padding: 0 !important;
                    cursor: pointer;
                }
                .resetbutton:hover{
                    background-color: #fff !important;
                }
            }
        }
    }
</style>